#include "enemy.h"
#include<QPixmap>
#include"GameSetting.h"
#include<QRandomGenerator>
#include<QGraphicsScene>
#include"player.h"
#include"bullet.h"
#include"score.h"
#include"health.h"

Enemy::Enemy(QGraphicsItem *parent):QGraphicsPixmapItem(parent)
{
    setPixmap(QPixmap(":/images/Enemy-removebg-preview.png"));
    setScale(GameSetting::EnemyScale);
    int max=GameSetting::SceneWidth-boundingRect().width()*GameSetting::EnemyScale;
    int randomNumber = QRandomGenerator::global()->bounded(1,max);
    setPos(randomNumber,0);
    startTimer(GameSetting::EnemyTimer);
}


void Enemy::timerEvent(QTimerEvent *)
{
    QList<QGraphicsItem *> collidingItems = this->collidingItems();
    for(auto item:collidingItems)
    {
        if(typeid(*item)==typeid(Player))
        {//生命值需要减少
            Health::getInstance().decrease();
            scene()->removeItem(this);
            delete this;
            return;
        }
        if(typeid(*item)==typeid(Bullet))
        {//需要加分
            Score::getInstance().increase();
            scene()->removeItem(item);
            scene()->removeItem(this);
            delete item;
            delete this;
            return;
        }
    }
    setPos(x(),y()+GameSetting::EnemySpeed);
    if(y()>GameSetting::SceneHeight)
    {
        scene()->removeItem(this);
        delete this;
    }
}
